namespace Spotted.Storage.Converters
{
    using System;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;

    public class BooleanVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Visibility.Collapsed;

            bool visibile;
            var reverse = false;

            if (!bool.TryParse(value.ToString(), out visibile))
                visibile = default(bool);

            if (parameter != null)
            {
                if (!bool.TryParse(parameter.ToString(), out reverse))
                    reverse = default(bool);
            }

            return !reverse
                    ? visibile ? Visibility.Visible : Visibility.Collapsed
                    : visibile ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}